package com.stormrage.edu.ctrl.index;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stormrage.edu.serv.Index.IdxTrialServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.index.IdxTrial;
import com.stormrage.entity.index.rep.IdxTrialRep;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: TrialController.java
 * @Package com.stormrage.edu.ctrl.index
 * @Description: 试看课件 Controller
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Controller
@RequestMapping("/trial")
public class IdxTrialCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(IdxTrialCtrl.class);

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.trial']}")
	private String trialPath;

	@Value("#{constants['path.thumb']}")
	private String thumbPath;

	@Autowired
	private IdxTrialServ idxTrialServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping("/")
	public String list(HttpServletRequest request, Model model) {

		logger.debug("【 Controller 】-【 TrialController 】-【 List 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取讲师List ------------------
			List<IdxTrialRep> array = idxTrialServ.queryRepList();

			// ------------------ Model 赋值 ------------------
			model.addAttribute("path", rootUrl + thumbPath);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 TrialController 】-【 List 】-【 GET 】-【 COMPLETED 】");
		return "index/trial/list";

	}

	@RequestMapping("/{id}")
	public String trial(HttpServletRequest request, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 TrialController 】-【 Trial 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取讲师信息 ------------------
			IdxTrial entity = idxTrialServ.queryById(id);
			List<IdxTrial> array = idxTrialServ.queryList();

			// ------------------ Model 赋值 ------------------
			model.addAttribute("thumbPath", rootUrl + thumbPath);
			model.addAttribute("trialPath", rootUrl + trialPath);
			model.addAttribute("data", entity);
			model.addAttribute("array", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 TrialController 】-【 Trial 】-【 GET 】-【 COMPLETED 】");
		return "index/trial/trial";

	}

}
